package com.bitdf.txing.users.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.valid.SaveGroup;
import com.bitdf.txing.common.valid.UpdateGroup;
import com.bitdf.txing.users.entity.UserPermissionEntity;
import com.bitdf.txing.users.service.UserPermissionService;
import com.bitdf.txing.users.vo.UserPermissionQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;



/**
 * 
 *
 * @author lizhiwei
 * @email 
 * @date 2023-09-02 21:43:03
 */
@RestController
@RequestMapping("${txing.request.path.users}/perm")
public class UserPermissionController {
    @Autowired
    private UserPermissionService userPermissionService;

    /**
     * 分页查询
     */
    @SaCheckLogin
    @SaCheckPermission("user:perm:list")
    @PostMapping("/list")
    public R list(@RequestBody UserPermissionQueryVo queryVo){
        PageUtils page = userPermissionService.queryPage(queryVo);
        return R.ok().put("data", page);
    }


    /**
     * 根据id获取
     */
    @SaCheckLogin
    @SaCheckPermission("user:perm:info:get")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		UserPermissionEntity userPermission = userPermissionService.getById(id);
        return R.ok().put("data", userPermission);
    }

    /**
     * 创建权限
     */
    @SaCheckLogin
    @SaCheckPermission("user:perm:save")
    @PostMapping("/save")
    public R save(@Validated(SaveGroup.class) @RequestBody UserPermissionEntity userPermission, BindingResult result){
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
        boolean save = userPermissionService.save(userPermission);
        return R.ok();
    }

    /**
     * 修改权限
     */
    @SaCheckLogin
    @SaCheckPermission("user:perm:update")
    @PostMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody UserPermissionEntity userPermission, BindingResult result){
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
		userPermissionService.updateById(userPermission);
        return R.ok();
    }

    /**
     * 删除权限
     */
    @SaCheckLogin
    @SaCheckPermission("user:perm:delete")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids){
		userPermissionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
